/*
    
    Copyright (C) 2011  Antoine

 	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
   	the Free Software Foundation; either version 3 of the License, or
   	(at your option) any later version.

   	This program is distributed in the hope that it will be useful,
   	but WITHOUT ANY WARRANTY; without even the implied warranty of
   	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   	GNU General Public License for more details.

   	You should have received a copy of the GNU General Public License
   	along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */
package subs;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.text.html.parser.Element;

public class MainController implements ActionListener {

	MainView			vue;
	BufferedWriter		ecrivain;
	Boolean				sync_display	= false;
	Boolean				modify_display	= false;
	Boolean				file_open		= false;

	Date				date_debut;
	Date				date_fin;
	long				duree_long;
	SimpleDateFormat	formatter		= new SimpleDateFormat("HH:mm:ss,SSS");

	public MainController(MainView vue1) {
		vue = vue1;
	}

	public void actionPerformed(ActionEvent e) {

		// R�action au clic sur Fermer
		if (e.getActionCommand().equals("Quit")) {
			int retour = JOptionPane.showConfirmDialog(vue, "Etes-vous sûr de vouloir quitter ?", "Information", JOptionPane.YES_NO_OPTION);
			if (retour == JOptionPane.YES_OPTION) {
				vue.dispose();
			}
		}

		// Reaction au clic sur Sauvegarder
		else if (e.getActionCommand().equals("Save")) {
			save(Sub.file);
		}
		else if (e.getActionCommand().equals("Open")) {
			JFileChooser fc = new JFileChooser();
			fc.setFileFilter(new FileNameExtensionFilter("Subtile srt", "srt"));

			int returnVal = fc.showDialog(vue, "Open");

			if (returnVal == JFileChooser.APPROVE_OPTION) {
				Lanceur.sub = new Sub(fc.getSelectedFile(), vue);
				Lanceur.sub.displayBoth();
				vue.enregistrer_sous.setEnabled(true);
				vue.enregistrer.setEnabled(true);
				vue.panel_modify.setVisible(true);
				vue.find.setEnabled(true);
			}
		}
		else if (e.getActionCommand().equals("About")) {
			JOptionPane.showMessageDialog(vue, "JSBE (C) Antoine 2011");
		}
		else if (e.getActionCommand().equals("save_as")) {
			JFileChooser fc = new JFileChooser();
			fc.setFileFilter(new FileNameExtensionFilter("Subtile srt", "srt"));

			int returnVal = fc.showSaveDialog(vue);

			if (returnVal == JFileChooser.APPROVE_OPTION) {

				save(fc.getSelectedFile());
			}
		}
		else if (e.getActionCommand().equals("Change")) {
			int rowIndex = vue.table.getSelectedRow();
			Calendar calendar_debut = Calendar.getInstance();

			try {

				Date debut_sub = formatter.parse(vue.debut_sub.getText());
				calendar_debut.setTime(debut_sub);
				int duree_finale = Integer.parseInt(vue.duree_sub.getValue().toString());
				calendar_debut.set(Calendar.MILLISECOND, calendar_debut.get(Calendar.MILLISECOND) + duree_finale);

			}
			catch (Exception ec) {
				ec.printStackTrace();
			}

			date_fin = calendar_debut.getTime();

			vue.table.setValueAt(vue.debut_sub.getText(), rowIndex, 1);
			vue.table.setValueAt(formatter.format(date_fin), rowIndex, 2);
			vue.table.setValueAt(vue.editor_texte.getText(), rowIndex, 3);
			Lanceur.sub.updatePane();
		}
		else if (e.getActionCommand().equals("Find")) {
			int result = Integer.parseInt(JOptionPane.showInputDialog(vue, "Enter a ID:"));
			vue.table.setRowSelectionInterval(result-1, result-1);
		}
	}

	public void save(File file) {
		try {
			String path = file.getPath();
			if (!path.endsWith(".srt")) path += ".srt";
			ecrivain = new BufferedWriter(new FileWriter(path));
			ecrivain.write(vue.editorPane.getText());
			ecrivain.close();
			JOptionPane.showMessageDialog(vue, "Fichier enregistré", "Message", JOptionPane.INFORMATION_MESSAGE);
		}
		catch (Exception exe) {

		}
	}

}
